package com.ms.service.module.fish.game;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Maps;
import com.ms.service.module.fish.bin.ConfAttackType;
import com.ms.service.module.fish.bin.ConfCommon;
import com.ms.service.module.fish.bin.ConfFish;
import com.ms.service.module.fish.bin.ConfLabaReward;
import com.ms.service.module.fish.bin.ConfLoadStage;
import com.ms.service.module.fish.bin.ConfScript;
import com.ms.service.module.fish.bin.ConfScriptTrai;
import com.ms.service.module.fish.bin.ConfSpecialBullet;
import com.ms.service.module.fish.bin.ConfStag;
import com.ms.service.module.fish.bin.FishSettingUtil;
import com.ms.service.module.fish.bin.ListTuple;
import com.ms.service.module.fish.utils.RandomUtils;

public class GameConf {

	
	private static Map<String,ConfCommon> confCommons;
	private static List<ConfLoadStage> loadStages;
	private static Map<Integer,ConfStag> stags;
	private static Map<Integer,ConfScript> scripts;
	private static Map<Integer,ConfScriptTrai> scriptTrais;
	private static Map<Integer,ConfFish> fishs;
	private static Map<Integer,ConfAttackType> attacks;
	private static Map<Integer,ConfSpecialBullet> bullets;
	private static List<ConfLabaReward> labas;
	
	public static void init() {
		
		// 触发场景
		initLoadStages();
		
		// 场景
		initStags();
		
		// 路径
		initScriptTrais();
		
		// 剧本
		initScripts();
		
		// 鱼类
		initFishs();
		
		// 攻击类型
		initAttackType();
		
		// 攻击类型
		initSpecBullet();
		
		// 初始化基础配置
		initCommons();
		
		// 初始化拉霸
		initLaba();
		
		// 初始化桌子
		for(int i=1;i<=20;i++) {
			FishGlobal.newDesk(i,0);
		}
		for(int i=1001;i<=1030;i++) {
			FishGlobal.newDesk(i,1);
		}
		for(int i=2001;i<=2004;i++) {
			FishGlobal.newDesk(i,2);
		}
		for(int i=5001;i<=5004;i++) {
			FishGlobal.newDesk(i,3);
		}
		
	}
	
	public static void initScriptTrais() {
		Map<Integer,ConfScriptTrai> scriptTraisT = Maps.newHashMap();
		List<ConfScriptTrai> cts = FishSettingUtil.getBin().getScriptTrais();
		for(ConfScriptTrai ct:cts){
			scriptTraisT.put(ct.getTraiId(), ct);
		}
		scriptTrais = scriptTraisT;
	}

	public static void initLaba() {
		labas = FishSettingUtil.getBin().getLabaRewards();
	}
	
	public static int getLabaReward(long sysCoin){
		for(ConfLabaReward stage:labas){
			if(sysCoin >= stage.getMlow() && sysCoin < stage.getMhigh()){
				return RandomUtils.nextInt(stage.getRlow(), stage.getRhigh());
			}
		}
		return 1;
	}

	public static void initAttackType() {
		Map<Integer,ConfAttackType> attackT = Maps.newHashMap();
		List<ConfAttackType> cts = FishSettingUtil.getBin().getAttackTypes();
		for(ConfAttackType ct:cts){
			attackT.put(ct.getAttType(), ct);
		}
		attacks = attackT;
	}
	
	public static ConfAttackType geAttackType(int type){
		return attacks.get(type);
	}
	
	public static void initSpecBullet() {
		Map<Integer,ConfSpecialBullet> bulletsT = Maps.newHashMap();
		List<ConfSpecialBullet> cts = FishSettingUtil.getBin().getSpecialBullets();
		for(ConfSpecialBullet ct:cts){
			bulletsT.put(ct.getBulType(), ct);
		}
		bullets = bulletsT;
	}
	public static ConfSpecialBullet getSpecialBullet(int type){
		return bullets.get(type);
	}


	public static void initCommons() {
		Map<String,ConfCommon> confCommonsT = Maps.newHashMap();
		List<ConfCommon> commons = FishSettingUtil.getBin().getCommons();
		for(ConfCommon comm:commons){
			confCommonsT.put(comm.getNid(), comm);
		}
		confCommons = confCommonsT;
		initConf();
	}

	public static void initLoadStages() {
		loadStages = FishSettingUtil.getBin().getLoadStages();
	}
	
	/**
	 * 通过当前系统资金加载场景
	 * @param sysMoney
	 * @return
	 */
	public static ConfStag getStage(long sysCoin){
		for(ConfLoadStage stage:loadStages){
			if(sysCoin >= stage.getMlow() && sysCoin < stage.getMhigh()){
				ArrayList<String> stages = stage.getStags();
				return stags.get(Integer.valueOf(stages.get(RandomUtils.nextInt(0, stages.size()))));
			}
		}
		return null;
	}
	
	/**
	 * 通过场景id获取场景信息
	 * @param stageId
	 * @return
	 */
	public static ConfStag getStageById(Integer stageId) {
		return stags.get(stageId);
	}
	
	public static void initStags() {
		Map<Integer,ConfStag> stagsT = Maps.newHashMap();
		List<ConfStag> cts = FishSettingUtil.getBin().getStages();
		for(ConfStag ct:cts){
			stagsT.put(ct.getStagId(), ct);
		}
		stags = stagsT;
		
	}
	
	public static void initScripts() {
		Map<Integer,ConfScript> scriptsT = Maps.newHashMap();
		List<ConfScript> cts = FishSettingUtil.getBin().getScripts();
		for(ConfScript ct:cts){
			scriptsT.put(ct.getScriptId(), ct);
		}
		scripts = scriptsT;
	}
	
	public static ConfScript getScript(int sid){
		return scripts.get(sid);
	}
	
	public static void initFishs() {
		Map<Integer,ConfFish> fishsT = Maps.newHashMap();
		List<ConfFish> cts = FishSettingUtil.getBin().getFishs();
		for(ConfFish ct:cts){
			fishsT.put(ct.getFishId(), ct);
		}
		fishs = fishsT;
		
	}
	
	public static ConfFish getFish(int fishId){
		return fishs.get(fishId);
	}
	
	public static String confJson;
	public static void initConfJson(){
		Map<String,Object> confs = Maps.newHashMap();
		confs.put("consume",baseConsume);
		confs.put("scripts", scripts.values());
		confs.put("scriptTrais", scriptTrais.values());
		confs.put("fishs", fishs.values());
		confs.put("attacks", attacks.values());
//		confJson = JsonUtils.object2JsonString(confs);
	}
	
	public static int version; // 配置版本
	public static int sysDayDec; // 系统每日扣除资金
	public static int sysInitMoney; // 系统初始投入资金
	public static int baseConsume ; // 基本消耗
	public static int rocketTriggerRate ; // 火箭炮触发概率，百分率
	public static int[] RSD_RATE ; // 燃烧弹倍率
	public static int deskSize ; // 桌子数
	public static ListTuple ldjxRewardRate ; // 雷电觉醒捕获鱼奖励配置
	public static int[] lyfbTriggerConf ; // 烈焰风暴触发规则配置
	
	public static void initConf() {
		if(FishGlobal.sysCurrHave == null){
			FishGlobal.sysCurrHave = new AtomicLong(getConfInt("sys_curr_have"));
		}
		
		sysDayDec = getConfInt("sys_day_dec");
		sysInitMoney = getConfInt("sys_init_money");
		baseConsume = getConfInt("base_consume");
		rocketTriggerRate = getConfInt("rocket_trigger_rate");
		ldjxRewardRate = new ListTuple(getConfStr("ldjx_reward_rate"));
		RSD_RATE = getConfInts("bult_rsd_rate");
		lyfbTriggerConf = getConfInts("lyfb_trigger_conf");
		if(deskSize == 0){
			deskSize = getConfInt("desk_size");
		}
		
		int versionT = getConfInt("conf_version");
		if(versionT > version){
			version = versionT;
			// 生成配置json
			initConfJson();
		}
	}
	
	public static int getConfInt(String nid){
		String v = getConfStr(nid);
		if(StringUtils.isNotBlank(v)){
			return Integer.parseInt(v);
		}
		return 0;
	}
	public static String getConfStr(String nid){
		ConfCommon common = confCommons.get(nid);
		if(common != null){
			return common.getValue();
		}
		return "";
	}

	public static int[] getConfInts(String nid){
		String v = getConfStr(nid);
		if(v != null){
			String[] strs = v.split(",");
			int[] result = new int[strs.length];
			for(int i=0;i<strs.length;i++){
				result[i] = Integer.parseInt(strs[i]);
			}
			return result;
		}
		return null;
	}

	
}
